package com.changgou.order.listener;


import com.alibaba.fastjson.JSON;
import com.changgou.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


@RabbitListener(queues = "queue.order") //用来表示监听那个队列,直接写死是pay微服务中的队列
@Component
public class PayOrderUpdateListener {


    @Autowired
    private OrderService orderService;

    @RabbitHandler  //表示用这个方法处理消息
    public void handlerMsg(String msg) {   //处理字符串的请求

        //1.接收消息(map对象的json字符串)
        Map<String, String> map = JSON.parseObject(msg, Map.class);

        if (map != null) {
            if (map.get("return_code").equals("SUCCESS")) {
                if (map.get("result_code").equals("SUCCESS")) {//支付成功
                    //2.更新订单的状态  update tb_order set sstat=1 where id=out_trade_no

                    String out_trade_no = map.get("out_trade_no");  //订单号
                    String time_end = map.get("time_end");           //订单更新时间
                    String transaction_id = map.get("transaction_id"); //微信订单号/流水号
                    orderService.updateStatus(out_trade_no, time_end, transaction_id);
                } else {
                    //支付失败---->删除订单
                    orderService.deleteOrder(map.get("out_trade_no"));
                }
            }
        }


    }

   /* @RabbitHandler  //表示用这个方法处理消息
    public  void handlerMsg(Map msg){   //表示用来处理map的请求



    }*/
}
